package list

import (
	. "lambda"
	. "bridge/base"
	"reduce"
)


// List structure

func Empty() Term {
	return Cons(Cons(FALSE, FALSE), FALSE)
}

func IsEmpty(list Term) Term {
	return App(App(Car(Car(list)), PrimByte(0)), PrimByte(1))
}

func PushFront(item, list Term) Term {
	return Cons(Cons(TRUE, item), list)
}

func Head(list Term) Term {
	return Cdr(Car(list))
}

func Tail(list Term) Term {
	return Cdr(list)
}

func Split(list Term) (car, cdr Term) {
	return Head(list), Tail(list)
}

func ReducePrimitiveList(list Term, env Environment) []Primitive {
	var out []Primitive

	for reduce.ReduceFully(IsEmpty(list), env)[0] == 0 {
		var car Term
		car, list = Split(list)
		prim := reduce.ReduceFully(car, env)
		out = append(out, prim)
	}

	return out
}
